{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import absolute_import, division, print_function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Elevation & Length of Day"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To account for the amount of sunlight avaialable to the plants at the different locations and times, get the length of the day. Here, I do this by using the 'Astral' package (https://pythonhosted.org/astral/index.html .\n",
    "\n",
    "I also leverage the Google Elevation API to retrieve the elevation for each location.\n",
    "\n",
    "The results are saved (and pickled) in dictionaries for easy lookup."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import pickle\n",
    "import time\n",
    "import requests \n",
    "import json\n",
    "import datetime\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from astral import Location"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cwd = os.getcwd()\n",
    "data = os.path.join(cwd,'data','wheat-2013-supervised.csv')\n",
    "df_2013 = pd.read_csv(data)\n",
    "data = os.path.join(cwd,'data','wheat-2014-supervised.csv')\n",
    "df_2014 = pd.read_csv(data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'6/3/2014 0:00'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_2013['Date'].max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'6/3/2015 0:00'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_2014['Date'].max()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Locations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1014"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_2013['Location'] = list(zip(df_2013['Longitude'], df_2013['Latitude']))\n",
    "locs_2013 = df_2013['Location'].unique().tolist()\n",
    "len(locs_2013)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1035"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_2014['Location'] = list(zip(df_2014['Longitude'], df_2014['Latitude']))\n",
    "locs_2014 = df_2014['Location'].unique().tolist()\n",
    "len(locs_2014)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Union of locations without repetitions\n",
    "elevation = {}\n",
    "for loc in locs_2013:\n",
    "    elevation[loc] = 0\n",
    "for loc in locs_2014:\n",
    "    if loc in elevation:\n",
    "        pass\n",
    "    else:\n",
    "        elevation[loc] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of unique locations across 2013 and 2014: 1167\n"
     ]
    }
   ],
   "source": [
    "print('Number of unique locations across 2013 and 2014: {}'.format(len(elevation.keys())))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Elevation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Get API key from system variable\n",
    "google_api_key = os.environ['GOOGLE_API_KEY']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "run_API_query = False\n",
    "\n",
    "base_string = 'https://maps.googleapis.com/maps/api/elevation/json?locations='\n",
    "locs = elevation.keys()\n",
    "\n",
    "if run_API_query is True:\n",
    "    for idx, loc in enumerate(locs):\n",
    "        lat_string = str(loc[1])\n",
    "        lng_string = str(loc[0])\n",
    "        call_string = base_string + lat_string + ',' + lng_string + '&key=' + google_api_key\n",
    "        if idx%10 == 0: \n",
    "            print(idx)\n",
    "        if elevation[loc] == 0:\n",
    "            response = requests.get(call_string)\n",
    "            try:\n",
    "                tmp_elevation = response.json()['results'][0]['elevation']\n",
    "                elevation[loc] = tmp_elevation\n",
    "            except:\n",
    "                print('Something went wrong', lat_string, lng_string)\n",
    "                print(response.json())\n",
    "            \n",
    "\n",
    "else:\n",
    "    # INSERT\n",
    "    #\n",
    "    # Load data saved at end of this notebook.\n",
    "    # Can be used to experiment with the Astral code below without\n",
    "    # having to re-run the Google API queries above.\n",
    "\n",
    "    with open(os.path.join('data','elevation.pickle'), 'rb') as handle:\n",
    "        elevation = pickle.load(handle)\n",
    "\n",
    "    \n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Length of day"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Use the 'Astral' package (https://pythonhosted.org/astral/index.html) to calculate the length of the day (~ hours of sunlight) for each location."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "locs = elevation.keys()\n",
    "length_of_day = {}\n",
    "for idx, loc in enumerate(locs):\n",
    "    # Initialize location\n",
    "    l = Location()\n",
    "    # Set attributes\n",
    "    l.name = ''\n",
    "    l.region = ''\n",
    "    l.latitude = loc[1]\n",
    "    l.longitude = loc[0]\n",
    "    l.timezone = 'UTC'\n",
    "    l.elevation = elevation[loc]\n",
    "    sun = l.sun(date=datetime.date(2014, 3, 3),local=True)\n",
    "    td = sun['sunset'] - sun['sunrise']\n",
    "    tmp_lod = td.total_seconds() / 3600. \n",
    "    length_of_day[loc] = tmp_lod \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Save data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Store data (serialize)\n",
    "with open(os.path.join('data','elevation.pickle'), 'wb') as handle:\n",
    "    pickle.dump(elevation, handle)\n",
    "\n",
    "with open(os.path.join('data','length_of_day.pickle'), 'wb') as handle:\n",
    "    pickle.dump(length_of_day, handle)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
